﻿Imports System.Windows
Imports System.Windows.Media
Imports System.IO

Public Class ViewDashboardZone
    Implements ComponentModel.INotifyPropertyChanged

   
    Dim _TitlePosition As ZoneTitlePosition
    Private Shared StyleProperties() As DependencyProperty


    Sub New()

        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        '   TitlePosition = ZoneTitlePosition.Bottom
    End Sub

    Public ReadOnly Property StandardBrushes As IEnumerable
        Get
            Return OMANE.Modules.Dashboarding.StandardBrushes.Items
        End Get
    End Property

    <ComponentModel.DefaultValue(CDbl(0))> _
    <ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property MarginTop As Double
        Get
            Return Margin.Top
        End Get
        Set(value As Double)
            Margin = New Thickness(Margin.Left, value, Margin.Right, Margin.Bottom)
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("MarginTop"))
        End Set
    End Property

    <ComponentModel.DefaultValue(CDbl(0))> _
    <ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property MarginLeft As Double
        Get
            Return Margin.Left
        End Get
        Set(value As Double)
            Margin = New Thickness(value, Margin.Top, Margin.Right, Margin.Bottom)
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("MarginLeft"))
        End Set
    End Property

    <ComponentModel.DefaultValue(CDbl(0))> _
    <ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property MarginBottom As Double
        Get
            Return Margin.Bottom
        End Get
        Set(value As Double)
            Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right, value)
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("MarginBottom"))
        End Set
    End Property

    <ComponentModel.DefaultValue(CDbl(0))> _
    <ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property MarginRight As Double
        Get
            Return Margin.Right
        End Get
        Set(value As Double)
            Margin = New Thickness(Margin.Left, Margin.Top, value, Margin.Bottom)
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("MarginRight"))
        End Set
    End Property

    <ComponentModel.DefaultValue(CInt(Windows.VerticalAlignment.Stretch))> _
    <ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property VAlign As Integer
        Get
            Return CInt(VerticalAlignment)
        End Get
        Set(value As Integer)
            VerticalAlignment = value
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("VAlign"))
        End Set
    End Property

    <ComponentModel.DefaultValue(CInt(Windows.HorizontalAlignment.Stretch))> _
    <ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property HAlign As Integer
        Get
            Return CInt(HorizontalAlignment)
        End Get
        Set(value As Integer)
            HorizontalAlignment = value
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("HAlign"))
        End Set
    End Property

    Public Property ZoneBackground As Brush
        Get
            Return GetValue(ZoneBackgroundProperty)
        End Get

        Set(ByVal value As Brush)
            SetValue(ZoneBackgroundProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ZoneBackgroundProperty As DependencyProperty = _
                           DependencyProperty.Register("ZoneBackground", _
                           GetType(Brush), GetType(ViewDashboardZone), _
                           New PropertyMetadata(Brushes.Violet))

    Public Property ZoneBorderBrush As Brush
        Get
            Return GetValue(ZoneBorderBrushProperty)
        End Get

        Set(ByVal value As Brush)
            SetValue(ZoneBorderBrushProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ZoneBorderBrushProperty As DependencyProperty = _
                           DependencyProperty.Register("ZoneBorderBrush", _
                           GetType(Brush), GetType(ViewDashboardZone), _
                           New PropertyMetadata(Brushes.Black))




    Public Property ZoneBorderThickness As Thickness
        Get
            Return GetValue(ZoneBorderThicknessProperty)
        End Get

        Set(ByVal value As Thickness)
            SetValue(ZoneBorderThicknessProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ZoneBorderThicknessProperty As DependencyProperty = _
                           DependencyProperty.Register("ZoneBorderThickness", _
                           GetType(Thickness), GetType(ViewDashboardZone), _
                           New PropertyMetadata(New Thickness(0)))




    Public Property ZoneCornerRadius As CornerRadius
        Get
            Return GetValue(ZoneCornerRadiusProperty)
        End Get

        Set(ByVal value As CornerRadius)
            SetValue(ZoneCornerRadiusProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ZoneCornerRadiusProperty As DependencyProperty = _
                           DependencyProperty.Register("ZoneCornerRadius", _
                           GetType(CornerRadius), GetType(ViewDashboardZone), _
                           New PropertyMetadata(New CornerRadius(0)))


    Public Property TitleVisibility As Boolean
        Get
            Return ccthumb.Visibility = Windows.Visibility.Visible
        End Get
        Set(value As Boolean)
            ccthumb.Visibility = If(value, Windows.Visibility.Visible, Windows.Visibility.Collapsed)
        End Set
    End Property


    Private _TitleStyle As StyleObject
    '<ComponentModel.DesignerSerializationVisibilityAttribute(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Property TitleStyle As StyleObject
        Get
            If _TitleStyle Is Nothing Then
                _TitleStyle = StyleObject.FromUIElement(ccthumb)
            End If
            Return _TitleStyle
        End Get
        Set(ByVal value As StyleObject)
            _TitleStyle = value
            If _TitleStyle IsNot Nothing Then
                _TitleStyle.Apply(ccthumb)
                'TitleXamlStyle = StyleObject.ToStyle(_TitleStyle, ccthumb)
                'ccthumb.Style = TitleXamlStyle
            End If
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("TitleStyle"))
        End Set
    End Property




    Public Property LayoutBlocked As Boolean
        Get
            Return GetValue(LayoutBlockedProperty)
        End Get

        Set(ByVal value As Boolean)
            SetValue(LayoutBlockedProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LayoutBlockedProperty As DependencyProperty = _
                           DependencyProperty.Register("LayoutBlocked", _
                           GetType(Boolean), GetType(ViewDashboardZone), _
                           New PropertyMetadata(False))




    Public Property ResizingBlocked As Boolean
        Get
            Return GetValue(ResizingBlockedProperty)
        End Get

        Set(ByVal value As Boolean)
            SetValue(ResizingBlockedProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ResizingBlockedProperty As DependencyProperty = _
                           DependencyProperty.Register("ResizingBlocked", _
                           GetType(Boolean), GetType(ViewDashboardZone), _
                           New PropertyMetadata(False))


    Public Property Title As String
        Get
            Return GetValue(TitleProperty)
        End Get

        Set(ByVal value As String)
            SetValue(TitleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly TitleProperty As DependencyProperty = _
                           DependencyProperty.Register("Title", _
                           GetType(String), GetType(ViewDashboardZone), _
                           New PropertyMetadata("(None)"))



    Public Property TitleXamlStyle As Style
        Get
            Return GetValue(TitleXamlStyleProperty)
        End Get

        Set(ByVal value As Style)
            SetValue(TitleXamlStyleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly TitleXamlStyleProperty As DependencyProperty = _
                           DependencyProperty.Register("TitleXamlStyle", _
                           GetType(Style), GetType(ViewDashboardZone), _
                           New PropertyMetadata(Nothing))







    <ComponentModel.DefaultValue(CInt(ZoneTitlePosition.Top))> _
    Public Property TitlePosition As ZoneTitlePosition
        Get
            Return _TitlePosition
        End Get
        Set(value As ZoneTitlePosition)
            _TitlePosition = value

            Dim t As RotateTransform = ccthumb.LayoutTransform

            Select Case _TitlePosition
                Case ZoneTitlePosition.Top
                    t.Angle = 0
                    Controls.Grid.SetRow(ccthumb, 0)
                    Controls.Grid.SetRowSpan(ccthumb, 1)
                    Controls.Grid.SetColumn(ccthumb, 0)
                    Controls.Grid.SetColumnSpan(ccthumb, 3)

                Case ZoneTitlePosition.Bottom
                    t.Angle = 0
                    Controls.Grid.SetRow(ccthumb, 2)
                    Controls.Grid.SetRowSpan(ccthumb, 1)
                    Controls.Grid.SetColumn(ccthumb, 0)
                    Controls.Grid.SetColumnSpan(ccthumb, 3)

                Case ZoneTitlePosition.Left
                    t.Angle = -90
                    Controls.Grid.SetRow(ccthumb, 0)
                    Controls.Grid.SetRowSpan(ccthumb, 3)
                    Controls.Grid.SetColumn(ccthumb, 0)
                    Controls.Grid.SetColumnSpan(ccthumb, 1)

                Case ZoneTitlePosition.Right
                    t.Angle = -90
                    Controls.Grid.SetRow(ccthumb, 0)
                    Controls.Grid.SetRowSpan(ccthumb, 3)
                    Controls.Grid.SetColumn(ccthumb, 2)
                    Controls.Grid.SetColumnSpan(ccthumb, 1)

            End Select
        End Set
    End Property

    Private _StylePropertiesOriginalValues() As Object
    Private Sub ShowOptionWindow()
        UndoRedoEngine.Instance.OpenGroup("Modification de la zone")
        DashboardZoneProperty._OptionsWindowOpen = True
        DashboardZoneProperty._OtpionsModifiedPropertiesOriginalValues = New SortedList(Of DashboardZoneProperty, Object)()
        DashboardZoneProperty._OtpionsModifiedPropertiesModifiedValues = New SortedList(Of DashboardZoneProperty, Object)()

        If StyleProperties Is Nothing Then
            StyleProperties = New DependencyProperty() { _
                    ViewDashboardZone.WidthProperty, _
                    ViewDashboardZone.HeightProperty, _
                    ViewDashboardZone.MarginProperty, _
                    ViewDashboardZone.HorizontalAlignmentProperty, _
                    ViewDashboardZone.VerticalAlignmentProperty, _
                    ViewDashboardZone.ZoneBackgroundProperty, _
                    ViewDashboardZone.ZoneBorderBrushProperty, _
                    ViewDashboardZone.ZoneBorderThicknessProperty, _
                    ViewDashboardZone.ZoneCornerRadiusProperty}
        End If
        _StylePropertiesOriginalValues = (From depprop In StyleProperties Select Me.GetValue(depprop)).ToArray()

        ModuleDashboarding._uiservice.ShowMessage(New ViewDashboardZoneOptions(Me))
        DashboardZoneProperty._OptionsWindowOpen = False

        Dim tempOtpionsModifiedPropertiesOriginalValues = DashboardZoneProperty._OtpionsModifiedPropertiesOriginalValues
        Dim tempOtpionsModifiedPropertiesModifiedValues = DashboardZoneProperty._OtpionsModifiedPropertiesModifiedValues

        Dim _StylePropertiesUndoRedoValues = (From i In Enumerable.Range(0, StyleProperties.Length)
                                              Where Not Object.Equals(Me.GetValue(StyleProperties(i)), _StylePropertiesOriginalValues(i))
                                              Select New Tuple(Of Integer, Object, Object)(i, Me.GetValue(StyleProperties(i)), _StylePropertiesOriginalValues(i))).ToArray()

        If tempOtpionsModifiedPropertiesModifiedValues.Count + tempOtpionsModifiedPropertiesOriginalValues.Count + _StylePropertiesUndoRedoValues.Length = 0 Then
            DashboardZoneProperty._OtpionsModifiedProperties = Nothing
            DashboardZoneProperty._OtpionsModifiedPropertiesOriginalValues = Nothing
            DashboardZoneProperty._OtpionsModifiedPropertiesModifiedValues = Nothing
            UndoRedoEngine.Instance.CancelGroup()
            Return
        End If

        UndoRedoEngine.Instance.Push( _
                    Sub()
                        DashboardZoneProperty._OptionsWindowOpen = False

                        For Each elt In _StylePropertiesUndoRedoValues
                            Me.SetValue(StyleProperties(elt.Item1), elt.Item2)
                        Next

                        For i = tempOtpionsModifiedPropertiesOriginalValues.Count - 1 To 0 Step -1
                            tempOtpionsModifiedPropertiesOriginalValues.Keys(i).Value = tempOtpionsModifiedPropertiesOriginalValues.Values(i)
                        Next


                    End Sub, _
                    Sub()
                        DashboardZoneProperty._OptionsWindowOpen = False

                        For Each elt In _StylePropertiesUndoRedoValues
                            Me.SetValue(StyleProperties(elt.Item1), elt.Item3)
                        Next

                        For i = tempOtpionsModifiedPropertiesModifiedValues.Count - 1 To 0 Step -1
                            tempOtpionsModifiedPropertiesModifiedValues.Keys(i).Value = tempOtpionsModifiedPropertiesModifiedValues.Values(i)
                        Next
                    End Sub, _
                    "Modification de la zone")

        DashboardZoneProperty._OtpionsModifiedProperties = Nothing
        DashboardZoneProperty._OtpionsModifiedPropertiesOriginalValues = Nothing
        DashboardZoneProperty._OtpionsModifiedPropertiesModifiedValues = Nothing
        UndoRedoEngine.Instance.CloseGroup()
    End Sub
    Private Sub Thumb_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        ShowOptionWindow()
    End Sub

    Private Sub Thumb_DragDelta(ByVal sender As System.Object, ByVal e As System.Windows.Controls.Primitives.DragDeltaEventArgs)
        If LayoutBlocked Then Return



        If sender Is cbthumb Then
            cbthumbdrag(e.HorizontalChange, e.VerticalChange)
        End If
        If sender Is ctthumb Then
            ctthumbdrag(e.HorizontalChange, e.VerticalChange)
        End If
        If sender Is lcthumb Then
            lcthumbdrag(e.HorizontalChange, e.VerticalChange)
        End If
        If sender Is rcthumb Then
            rcthumbdrag(e.HorizontalChange, e.VerticalChange)
        End If
        If sender Is ccthumb Then
            ccthumbdrag(e.HorizontalChange, e.VerticalChange)
        End If

    End Sub

    Private Sub cbthumbdrag(ByVal HorizontalChange As Double, ByVal VerticalChange As Double)
        Select Case Me.VerticalAlignment
            Case Windows.VerticalAlignment.Center
                If Double.IsNaN(Height) Then
                    Height = ActualHeight
                End If
                Height += VerticalChange

            Case Windows.VerticalAlignment.Top
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right, 0.0)
                If Double.IsNaN(Height) Then
                    Height = ActualHeight
                End If
                Height += VerticalChange

            Case Windows.VerticalAlignment.Bottom
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right, Margin.Bottom - VerticalChange)
                If Double.IsNaN(Height) Then
                    Height = ActualHeight
                End If
                Height += VerticalChange

            Case Windows.VerticalAlignment.Stretch
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right, Margin.Bottom - VerticalChange)

        End Select
    End Sub

    Private Sub ctthumbdrag(ByVal HorizontalChange As Double, ByVal VerticalChange As Double)
        Select Case Me.VerticalAlignment
            Case Windows.VerticalAlignment.Center
                If Double.IsNaN(Height) Then
                    Height = ActualHeight
                End If
                Height -= VerticalChange

            Case Windows.VerticalAlignment.Top
                Margin = New Thickness(Margin.Left, Margin.Top + VerticalChange, Margin.Right, Margin.Bottom)
                If Double.IsNaN(Height) Then
                    Height = ActualHeight
                End If
                Height -= VerticalChange

            Case Windows.VerticalAlignment.Bottom
                Margin = New Thickness(Margin.Left, 0.0, Margin.Right, Margin.Bottom)
                If Double.IsNaN(Height) Then
                    Height = ActualHeight
                End If
                Height -= VerticalChange

            Case Windows.VerticalAlignment.Stretch
                Margin = New Thickness(Margin.Left, Margin.Top + VerticalChange, Margin.Right, Margin.Bottom)

        End Select
    End Sub

    Private Sub lcthumbdrag(ByVal HorizontalChange As Double, ByVal VerticalChange As Double)
        Select Case Me.HorizontalAlignment
            Case Windows.HorizontalAlignment.Center
                If Double.IsNaN(Width) Then
                    Width = ActualWidth
                End If
                Width -= HorizontalChange

            Case Windows.HorizontalAlignment.Left
                Margin = New Thickness(Margin.Left + HorizontalChange, Margin.Top, Margin.Right, Margin.Bottom)
                If Double.IsNaN(Width) Then
                    Width = ActualWidth
                End If
                Width -= HorizontalChange

            Case Windows.HorizontalAlignment.Right
                Margin = New Thickness(0.0, Margin.Top, Margin.Right, Margin.Bottom)
                If Double.IsNaN(Width) Then
                    Width = ActualWidth
                End If
                Width -= HorizontalChange

            Case Windows.HorizontalAlignment.Stretch
                Margin = New Thickness(Margin.Left + HorizontalChange, Margin.Top, Margin.Right, Margin.Bottom)

        End Select
    End Sub

    Private Sub rcthumbdrag(ByVal HorizontalChange As Double, ByVal VerticalChange As Double)
        Select Case Me.HorizontalAlignment
            Case Windows.HorizontalAlignment.Center
                If Double.IsNaN(Width) Then
                    Width = ActualWidth
                End If
                Width += HorizontalChange

            Case Windows.HorizontalAlignment.Left
                Margin = New Thickness(Margin.Left, Margin.Top, 0.0, Margin.Bottom)
                If Double.IsNaN(Width) Then
                    Width = ActualWidth
                End If
                Width += HorizontalChange

            Case Windows.HorizontalAlignment.Right
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right - HorizontalChange, Margin.Bottom)
                If Double.IsNaN(Width) Then
                    Width = ActualWidth
                End If
                Width += HorizontalChange

            Case Windows.HorizontalAlignment.Stretch
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right - HorizontalChange, Margin.Bottom)

        End Select
    End Sub

    Private Sub ccthumbdrag(ByVal HorizontalChange As Double, ByVal VerticalChange As Double)

        Select Case Me.HorizontalAlignment
            Case Windows.HorizontalAlignment.Center
                Margin = New Thickness(Margin.Left + HorizontalChange / 2.0, Margin.Top, Margin.Right - HorizontalChange / 2.0, Margin.Bottom)

            Case Windows.HorizontalAlignment.Left
                Margin = New Thickness(Margin.Left + HorizontalChange, Margin.Top, Margin.Right, Margin.Bottom)

            Case Windows.HorizontalAlignment.Right
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right - HorizontalChange, Margin.Bottom)

            Case Windows.HorizontalAlignment.Stretch
                Margin = New Thickness(Margin.Left + HorizontalChange, Margin.Top, Margin.Right - HorizontalChange, Margin.Bottom)

        End Select


        Select Case Me.VerticalAlignment
            Case Windows.VerticalAlignment.Center
                Margin = New Thickness(Margin.Left, Margin.Top + VerticalChange / 2.0, Margin.Right, Margin.Bottom - VerticalChange / 2.0)

            Case Windows.VerticalAlignment.Top
                Margin = New Thickness(Margin.Left, Margin.Top + VerticalChange, Margin.Right, Margin.Bottom)

            Case Windows.VerticalAlignment.Bottom
                Margin = New Thickness(Margin.Left, Margin.Top, Margin.Right, Margin.Bottom - VerticalChange)

            Case Windows.VerticalAlignment.Stretch
                Margin = New Thickness(Margin.Left, Margin.Top + VerticalChange, Margin.Right, Margin.Bottom - VerticalChange)

        End Select


    End Sub

    Private Shared _MultiValueVisibilityConverter As New CustomVisibilityConverter
    Public Shared ReadOnly Property MultiValueVisibilityConverter As Data.IMultiValueConverter
        Get
            Return _MultiValueVisibilityConverter
        End Get
    End Property

    Protected Overrides Function ShouldSerializeProperty(dp As DependencyProperty) As Boolean
        Dim acceptableproperties = New String() { _
            "ZoneBackground", _
            "ZoneBorderBrush", _
            "ZoneBorderThickness", _
            "ZoneCornerRadius", _
            "HorizontalAlignment", _
            "VerticalAlignment", _
            "Margin", _
            "Width", _
            "Height", _
            "DataContext"}

        Return acceptableproperties.Contains(dp.Name)
    End Function

    Private Sub ModifierContextMenuItem_Click(sender As Object, e As RoutedEventArgs) Handles ModifierContextMenuItem.Click
        ShowOptionWindow()
    End Sub

    Public Event PropertyChanged(sender As Object, e As ComponentModel.PropertyChangedEventArgs) Implements ComponentModel.INotifyPropertyChanged.PropertyChanged

    Private Sub CopierContextMenuItem_Click(sender As Object, e As RoutedEventArgs) Handles CopierContextMenuItem.Click

        Dim dobject = New DataObject()

        Dim bitmap = New Media.Imaging.RenderTargetBitmap( _
                     CInt(Math.Round(ActualWidth)), _
                     CInt(Math.Round(ActualHeight)), _
                     96, 96, Media.PixelFormats.Pbgra32)

        Dim dv = New DrawingVisual()
        Using dc As System.Windows.Media.DrawingContext = dv.RenderOpen()
            Dim vb = New VisualBrush(Me)
            dc.DrawRectangle(vb, Nothing, New Rect(New Point(), New Size(CInt(Math.Round(ActualWidth)), CInt(Math.Round(ActualHeight)))))
        End Using

        bitmap.Render(dv)

        dobject.SetImage(bitmap)

        Dim x = <CopyData/>
        Try
            Me.Save(x)

            dobject.SetText(x.ToString())

            dobject.SetData(x)
        Catch ex As Exception

        End Try




        Dim dz = TryCast(Me.DataContext, DashboardZone)

        For Each prop In dz.Properties
            If prop.Name.ToLower() = "data" Then
                Dim propdv = TryCast(prop.Value, DataView)

                Dim str = ""
                For i = 0 To propdv.Table.Columns.Count - 1
                    If i < propdv.Table.Columns.Count - 1 Then
                        str = str & propdv.Table.Columns(i).ColumnName & ";"
                    Else
                        str = str & propdv.Table.Columns(i).ColumnName
                    End If
                Next

                For i = 0 To propdv.Count - 1
                    str = str & vbCrLf
                    For j = 0 To propdv.Table.Columns.Count - 1
                        If j < propdv.Table.Columns.Count - 1 Then
                            If propdv.Table.Columns(j).DataType Is GetType(String) Then
                                str = str & """" & propdv(i)(j).ToString() & """;"
                            Else
                                str = str & propdv(i)(j).ToString() & ";"
                            End If
                        Else
                            If propdv.Table.Columns(j).DataType Is GetType(String) Then
                                str = str & propdv(i)(j).ToString() & ";"
                            Else
                                str = str & propdv(i)(j).ToString()
                            End If
                        End If
                    Next
                Next

                dobject.SetText(str, TextDataFormat.CommaSeparatedValue)
            End If
        Next

        Clipboard.SetDataObject(dobject, True)
    End Sub

    Private _DragStartedData As Object()
    Private Sub Thumb_DragStarted(sender As Object, e As Controls.Primitives.DragStartedEventArgs)
        _DragStartedData = New Object() {Me.Margin, Me.Width, Me.Height}
        UndoRedoEngine.Instance.OpenGroup("Redimensionnement de la zone (" & sender.Name & ")")
    End Sub

    Private Sub Thumb_DragCompleted(sender As Object, e As Controls.Primitives.DragCompletedEventArgs)
        Dim _DragCompletedData = New Object() {Me.Margin, Me.Width, Me.Height}

        If Object.Equals(_DragStartedData(0), _DragCompletedData(0)) And _
            Object.Equals(_DragStartedData(1), _DragCompletedData(1)) And _
             Object.Equals(_DragStartedData(2), _DragCompletedData(2)) Then

            UndoRedoEngine.Instance.CancelGroup()
        End If

        Dim temp = _DragStartedData.Clone()

        UndoRedoEngine.Instance.Push( _
            Sub()
                Me.Margin = temp(0)
                Me.Width = temp(1)
                Me.Height = temp(2)
            End Sub, _
            Sub()
                Me.Margin = _DragCompletedData(0)
                Me.Width = _DragCompletedData(1)
                Me.Height = _DragCompletedData(2)
            End Sub, _
            "Redimensionnement de la zone  (" & sender.Name & ")")

        UndoRedoEngine.Instance.CloseGroup()
    End Sub

    Public Sub Save(ByVal x As XElement)
        Dim zonecontainer = <Zone
                                TypeFullName=<%= DataContext.GetType().FullName %>
                                TypeAssemblyQualifiedName=<%= DataContext.GetType().AssemblyQualifiedName %>
                                Row=<%= Controls.Grid.GetRow(Me) %>
                                RowSpan=<%= Controls.Grid.GetRowSpan(Me) %>
                                Column=<%= Controls.Grid.GetColumn(Me) %>
                                ColumnSpan=<%= Controls.Grid.GetColumnSpan(Me) %>
                                Name=<%= Name %>
                                Title=<%= Title %>
                                TitleVisibility=<%= TitleVisibility %>
                                LayoutBlocked=<%= LayoutBlocked %>
                                ResizingBlocked=<%= ResizingBlocked %>
                            />


        '
        Dim xamlzonewriter = New IO.StringWriter()
        System.Windows.Markup.XamlWriter.Save(Me, xamlzonewriter)
        zonecontainer.Add(XElement.Parse(xamlzonewriter.ToString()))
        x.Add(zonecontainer)
        xamlzonewriter.Close()
        xamlzonewriter.Dispose()
        xamlzonewriter = Nothing

        Dim zonevisual = CType(DataContext, DashboardZone).Visual

        Dim zonevisualcontainer = <ZoneVisual
                                      TypeFullName=<%= zonevisual.GetType().FullName %>
                                      TypeAssemblyQualifiedName=<%= zonevisual.GetType().AssemblyQualifiedName %>
                                  />

        xamlzonewriter = New IO.StringWriter()
        System.Windows.Markup.XamlWriter.Save(zonevisual, xamlzonewriter)
        zonevisualcontainer.Add(XElement.Parse(xamlzonewriter.ToString()))
        zonecontainer.Add(zonevisualcontainer)

        xamlzonewriter.Close()
        xamlzonewriter.Dispose()
        xamlzonewriter = Nothing
    End Sub
End Class

Public Enum ZoneTitlePosition
    Top
    Left
    Bottom
    Right
End Enum

Public Class ZoneTitlePositionValues
    Public Shared ReadOnly Property Items As IEnumerable
        Get
            Return New ZoneTitlePosition() {ZoneTitlePosition.Top, ZoneTitlePosition.Bottom, ZoneTitlePosition.Left, ZoneTitlePosition.Right}
        End Get
    End Property

End Class

Public Class CustomVisibilityConverter
    Implements Data.IMultiValueConverter

    Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Data.IMultiValueConverter.Convert
        Try
            If CBool(values(1)) Or CBool(values(2)) Then Return Windows.Visibility.Collapsed
            If CBool(values(0)) Then Return Windows.Visibility.Visible
        Catch ex As Exception
            Return Windows.Visibility.Collapsed
        End Try

        Return Windows.Visibility.Collapsed
    End Function

    Public Function ConvertBack(value As Object, targetTypes() As Type, parameter As Object, culture As Globalization.CultureInfo) As Object() Implements Data.IMultiValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class
